import { Component, OnInit } from '@angular/core';
import { LocalStorageService } from '../../service/localStorageService/local-storage.service';
import { memberSelfUrl, signOutUrl,memberImgUrl } from '../../urlConfig';
import { HttpService } from '../../service/httpService/http.service';
import { Router } from '@angular/router';

@Component({
  selector: 'app-home',
  templateUrl: './home.component.html',
  styleUrls: ['./home.component.styl']
})
export class HomeComponent implements OnInit {
  constructor(public localStorage: LocalStorageService, public http: HttpService, private router: Router, ) { }
  isCollapsed = false;
  openMap: { [name: string]: boolean } = {
    sub1: true,
    sub2: true,
  };
  memberImgUrl:string = memberImgUrl; // 图片地址前缀
  name: '';
  username: '';
  image:'';
  routingList: any[] = []; // 权限路由
  commonRoutingList: any[] = [ // 普通路由
    { routing: 'personal-data', name: '个人资料' },
    {routing:'resetpassword',name:'修改密码'},
    { routing: 'individualopus', name: '发布的作品' },
    { routing: 'shareresource', name: '分享的资源' },
  ];
  administrator: any[] = [
    { // 超级权限
      memberManagement: '成员管理',
      sublist: [{
        routing: 'member-application',
        name: '新成员申请',
      },
      {
        routing: 'member',
        name: '工作室成员',
      }]
    }];

  root: any[] = [{ // 中级权限
    memberManagement: '成员管理',
    sublist: [{
      routing: 'member-application',
      name: '新成员申请',
    },
    {
      routing: 'member',
      name: '工作室成员',
    }]
  }];

  general: any[] = [
    {
      memberManagement: '成员管理',
      sublist: [{
        routing: 'member-application',
        name: '新成员申请',
      },
      {
        routing: 'member',
        name: '工作室成员',
      }]
    }
  ];  // 普通权限




  ngOnInit() {

    // 获取个人信息
    this.http.getData(memberSelfUrl).subscribe((event: any) => {
      console.log(event);

      if (event.code === 200) {
        this.name = event.data.name;
        this.username = event.data.username;
        this.image = event.data.image;
      }

    })


    // 路由显示控制
    var token: any = this.localStorage.getItem('token');
    if (token.role === "administrator") {
      this.routingList = this.administrator;
    } else if (token.role === "root") {
      this.routingList = this.root;
    } else if (token.role === "general") {
      this.routingList = this.general;
    }
  }

  outLogin() {

    this.http.getData(signOutUrl).subscribe((event: any) => {
      if (event.code === 200) {
        this.localStorage.removeItem('token');
        this.router.navigate(['/login']);
      }
    })
  }

}
